package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.pojo.Article;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.PetService;
import com.itheima.mapper.PetServiceMapper;
import com.itheima.service.PetServiceService;
import com.itheima.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class PetServiceServiceImpl implements PetServiceService {
    @Autowired
    private PetServiceMapper petServiceMapper;

    @Override
    public void add(PetService petService){
        petService.setCreateTime(LocalDateTime.now());
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        petService.setCreateUser(userId);
        petServiceMapper.add(petService);
    }
    @Override
    public PageBean<PetService> list(Integer pageNum, Integer pageSize,String username) {
        //1.创建PageBean对象
        PageBean<PetService> pb = new PageBean<>();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3.调用mapper
        Map<String,Object> map = ThreadLocalUtil.get();
        List<PetService> as = petServiceMapper.list(username);
        //Page中提供了方法，可以获取PageHelper分页查询后，得到的总记录条数和当前页数据
        Page<PetService> p =(Page<PetService>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }
    @Override
    public List<PetService> list2(){
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        return petServiceMapper.list2(userId);
    }
}
